<!DOCTYPE html>
<html lang="zh-Hans">
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="王诗翔" />



<meta name="description" content="Shell脚本笔记系列： 构建基本shell脚本 Linux结构化命令  内容：  传递参数 跟踪参数 移动变量 处理选项 将选项标准化 获得用户输入   经过前面的介绍，我们已经可以掌握一些流程化的脚本编程了。但有时候，我们需要编写的脚本能够跟使用者进行交互。它可以是静态的，输入相应的参数让它运行到底；也可以是动态的，脚本根据输入参数反馈不同的信息，使用者又能根据信息调整下一步的处理，实时与程序">
<meta name="keywords" content="bash shell,shell笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="Shell脚本之处理用户输入">
<meta property="og:url" content="https://shixiangwang.github.io/2017/08/19/working-with-user-input/index.html">
<meta property="og:site_name" content="Cloudberry">
<meta property="og:description" content="Shell脚本笔记系列： 构建基本shell脚本 Linux结构化命令  内容：  传递参数 跟踪参数 移动变量 处理选项 将选项标准化 获得用户输入   经过前面的介绍，我们已经可以掌握一些流程化的脚本编程了。但有时候，我们需要编写的脚本能够跟使用者进行交互。它可以是静态的，输入相应的参数让它运行到底；也可以是动态的，脚本根据输入参数反馈不同的信息，使用者又能根据信息调整下一步的处理，实时与程序">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-01-27T04:08:11.150Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Shell脚本之处理用户输入">
<meta name="twitter:description" content="Shell脚本笔记系列： 构建基本shell脚本 Linux结构化命令  内容：  传递参数 跟踪参数 移动变量 处理选项 将选项标准化 获得用户输入   经过前面的介绍，我们已经可以掌握一些流程化的脚本编程了。但有时候，我们需要编写的脚本能够跟使用者进行交互。它可以是静态的，输入相应的参数让它运行到底；也可以是动态的，脚本根据输入参数反馈不同的信息，使用者又能根据信息调整下一步的处理，实时与程序">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">


    <link rel="alternate" href="/atom.xml" title="Cloudberry" type="application/atom+xml">



    <link rel="shortcut icon" href="/favicon.png">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="../../../../css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>Shell脚本之处理用户输入 | Cloudberry</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: true
    }
</script>


    <script>
        yiliaConfig.jquery_ui = [true, "//cdn.bootcss.com/jqueryui/1.10.4/jquery-ui.min.js", "//cdn.bootcss.com/jqueryui/1.10.4/css/jquery-ui.min.css"];
    </script>



    <script> yiliaConfig.rootUrl = "\/";</script>






</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            <img src="/img/avatar.png" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/">王诗翔</a></h1>
        </hgroup>

        
        <p class="header-subtitle">A site for writing life</p>
        

        
            <form id="search-form">
            <input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="false" />
            <i class="fa fa-times" onclick="resetSearch()"></i>
            </form>
            <div id="local-search-result"></div>
            <p class='no-result'>No results found <i class='fa fa-spinner fa-pulse'></i></p>
        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="../../../../index.html">主页</a></li>
                        
                            <li><a href="../../../../archives/">所有文章</a></li>
                        
                            <li><a href="../../../../tags/">标签云</a></li>
                        
                            <li><a href="../../../../about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:w_shixiang@163.com" title="Email"></a>
                            
                                <a class="fa GitHub" href="https://github.com/ShixiangWang" title="GitHub"></a>
                            
                                <a class="fa RSS" href="../../../../atom.xml" title="RSS"></a>
                            
                                <a class="fa 知乎" href="https://www.zhihu.com/people/wang-shi-xiang-65/activities" title="知乎"></a>
                            
                                <a class="fa 简书" href="https://www.jianshu.com/u/b6608e27dc74" title="简书"></a>
                            
                                <a class="fa Twitter" href="https://twitter.com/WangShxiang" title="Twitter"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/Gist/">Gist</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/Linux-shell/">Linux shell</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/OOP/">OOP</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/R/">R</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/Rmarkdown/">Rmarkdown</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/Rmd/">Rmd</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/Rstudio/">Rstudio</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/bash-shell/">bash shell</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/fasta/">fasta</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/git/">git</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/github/">github</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/lapply/">lapply</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/linux/">linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/markdown/">markdown</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/md/">md</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/python/">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/sapply/">sapply</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/sed/">sed</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/shell笔记/">shell笔记</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/survival/">survival</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/survminer/">survminer</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/vapply/">vapply</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/写作/">写作</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/数据处理/">数据处理</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/文本处理/">文本处理</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/生存分析/">生存分析</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/生物信息学/">生物信息学</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/类/">类</a></li><li class="tag-list-item"><a class="tag-list-link" href="../../../../tags/迭代计算/">迭代计算</a></li></ul>
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a class="main-nav-link switch-friends-link" href="http://www.ruanyifeng.com/blog/archives.html">阮一峰的网络日志</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://blog.fens.me/series-r/">R的极客理想系列</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://yihui.name/">谢益辉</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://www.huangshujia.me/">解螺旋的矿工</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://github.com/GuangchuangYu">GuangchuangYu</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://vip.biotrainee.com/">生信技能树</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">人该是自己生活的主宰，而不是别人手中的行货</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">王诗翔</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                <img src="/img/avatar.png" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">王诗翔</a></h1>
            </hgroup>
            
            <p class="header-subtitle">A site for writing life</p>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="../../../../index.html">主页</a></li>
                
                    <li><a href="../../../../archives/">所有文章</a></li>
                
                    <li><a href="../../../../tags/">标签云</a></li>
                
                    <li><a href="../../../../about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:w_shixiang@163.com" title="Email"></a>
                            
                                <a class="fa GitHub" target="_blank" href="https://github.com/ShixiangWang" title="GitHub"></a>
                            
                                <a class="fa RSS" target="_blank" href="../../../../atom.xml" title="RSS"></a>
                            
                                <a class="fa 知乎" target="_blank" href="https://www.zhihu.com/people/wang-shi-xiang-65/activities" title="知乎"></a>
                            
                                <a class="fa 简书" target="_blank" href="https://www.jianshu.com/u/b6608e27dc74" title="简书"></a>
                            
                                <a class="fa Twitter" target="_blank" href="https://twitter.com/WangShxiang" title="Twitter"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="标签" friends="友情链接" about="关于我"/>
</nav>
      <div class="body-wrap"><article id="post-working-with-user-input" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="" class="article-date">
      <time datetime="2017-08-18T16:00:00.000Z" itemprop="datePublished">2017-08-19</time>
</a>


    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Shell脚本之处理用户输入
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
    <div class="article-category tagcloud">
    <a class="article-category-link" href="../../../../categories/Linux杂烩/">Linux杂烩</a><a class="article-category-link" href="../../../../categories/Linux杂烩/shell编程/">shell编程</a>
    </div>


        
    <div class="article-tag tagcloud">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="../../../../tags/bash-shell/">bash shell</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="../../../../tags/shell笔记/">shell笔记</a></li></ul>
    </div>

        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <p><strong>Shell脚本笔记系列：</strong></p>
<p><a href="http://www.flypeom.site/linux/2017/08/11/basic-shell/" target="_blank" rel="noopener">构建基本shell脚本</a></p>
<p><a href="http://www.flypeom.site/linux/2017/08/11/structural-command-of-shell/" target="_blank" rel="noopener">Linux结构化命令</a></p>
<blockquote>
<p>内容：</p>
<ul>
<li>传递参数</li>
<li>跟踪参数</li>
<li>移动变量</li>
<li>处理选项</li>
<li>将选项标准化</li>
<li>获得用户输入</li>
</ul>
</blockquote>
<p>经过前面的介绍，我们已经可以掌握一些流程化的脚本编程了。但有时候，我们需要编写的脚本能够跟使用者进行交互。它可以是静态的，输入相应的参数让它运行到底；也可以是动态的，脚本根据输入参数反馈不同的信息，使用者又能根据信息调整下一步的处理，实时与程序互动。</p>
<p>bash shell提供了一些不同的方法来从<strong>用户处获得数据，包括命令行参数、命令行选项以及直接从键盘读取输入</strong>的能力。下面将一一介绍实现。</p>
<a id="more"></a>
<h2 id="命令行参数"><a href="#命令行参数" class="headerlink" title="命令行参数"></a>命令行参数</h2><p>使用命令行参数是向脚本传递数据的最基本方法，在运行脚本的同时可以在命令行添加数据。</p>
<p>比如：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./addem 10 30</span><br></pre></td></tr></table></figure>
<p>运行当前目录下名为<code>addem</code>脚本的同时向内部传递2个参数（10和30）。而脚本会通过特殊的变量来处理命令行参数。</p>
<p>下面介绍如何使用它们。</p>
<h3 id="读入参数"><a href="#读入参数" class="headerlink" title="读入参数"></a>读入参数</h3><p>bash shell会将称为<strong>位置参数</strong>的特殊变量分配给输入到命令行的所有参数：<code>$0</code>是程序名，<code>$1</code>是第一个参数，<code>$2</code>是第二个参数，以此类推。书上介绍直到第九个参数<code>$9</code>，但这个应该不是受限的。<a href="http://www.cnblogs.com/ivictor/p/4022382.html" target="_blank" rel="noopener">Shell最多可以输入多少个参数？</a>一文探索了参数的个数限制，有兴趣的朋友不妨看看和试试。</p>
<p>下面是单个参数的例子：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx-ubuntu:~/script_learn$ cat test1.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> using one <span class="built_in">command</span> line parameter</span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">factorial=1</span><br><span class="line">for (( number = 1; number &lt;= $1; number++ ))</span><br><span class="line">do</span><br><span class="line">truefactorial=$[ $factorial * $number ]</span><br><span class="line">done</span><br><span class="line">echo The factorial of $1 is $factorial</span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ ./test1.sh 5</span><br><span class="line">The factorial of 5 is 120</span><br></pre></td></tr></table></figure>
<p>我们可以像使用其他变量一样使用<code>$1</code>变量。shell脚本会自动分配，不需要我们做任何处理。可以看得出来这样非常方便，不过如果输入参数过多，很容易让人混淆。</p>
<p>如果需要输入更多的参数，只需要用空格分隔即可。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx-ubuntu:~/script_learn$ cat test2.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> using two <span class="built_in">command</span> line parameters</span></span><br><span class="line"></span><br><span class="line">total=$[ $1 * $2 ]</span><br><span class="line">echo The first parameter is $1.</span><br><span class="line">echo The second parameter is $2.</span><br><span class="line">echo The total value is $total.</span><br><span class="line"></span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ ./test2.sh 2 5</span><br><span class="line">The first parameter is 2.</span><br><span class="line">The second parameter is 5.</span><br><span class="line">The total value is 10.</span><br></pre></td></tr></table></figure>
<p>字符参数也是一样的。如果我没记错，在脚本中的数字默认都是做字符处理的，进行数学运算时会自动调整。不过当我们想要输入的一个参数是带空格的字符串时，需要在两边加上引号以保证shell能够正确识别。不然会被当做多个参数处理的。</p>
<p>下面取个例子：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx-ubuntu:~/script_learn$ cat test3.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">echo Hello $1, glad to meet you.</span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ ./test3.sh shixiang wang</span><br><span class="line">Hello shixiang, glad to meet you.</span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ ./test3.sh 'shixiang wang'</span><br><span class="line">Hello shixiang wang, glad to meet you.</span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ ./test3.sh "shixiang wang"</span><br><span class="line">Hello shixiang wang, glad to meet you.</span><br></pre></td></tr></table></figure>
<p>看来使用时要注意正确使用引号喔。</p>
<blockquote>
<p>说明：在文本字符串作为参数传递时，引号并非数据的一部分。它们只是表明数据的起止位置。</p>
</blockquote>
<p>如果需要输入的命令行参数不止9个，写脚本需要修改一下变量名（第9个之后）。比如<code>${10}</code>表示输入的第十个变量（原来如此啊）。</p>
<p>这样我们就可以向脚本添加任意多的参数啦～</p>
<p>前面也说了，<code>$0</code>参数可以获取脚本名，这样在编程的时候很方便。但是这里存在一个潜在的问题：如果使用另一个命令来运行shell脚本，命令会和脚本名混在一起，出现在<code>$0</code>中。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx-ubuntu:~/script_learn$ cat test4.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> Testing the <span class="variable">$0</span> parameter</span></span><br><span class="line"></span><br><span class="line">echo The zero parameter is set to: $0</span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ ./test4.sh</span><br><span class="line">The zero parameter is set to: ./test4.sh</span><br><span class="line"></span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ bash test4.sh</span><br><span class="line">The zero parameter is set to: test4.sh</span><br><span class="line"></span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ bash ~/script_learn/test4.sh</span><br><span class="line">The zero parameter is set to: /home/wsx/script_learn/test4.sh</span><br></pre></td></tr></table></figure>
<p>而且，你发现没有，当指明脚本路径时，这个路径也会带入其中。</p>
<p>如果我们要编写一个根据脚本名来执行不同功能的脚本，就得把脚本的运行路径剥离掉。还要删除与脚本名混在一起的命令。幸好有一个方便的小命令<code>basename</code>可以帮助我们返回不包含路径的脚本名。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx-ubuntu:~/script_learn$ cat test5.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> Using basename with the <span class="variable">$0</span> parameter</span></span><br><span class="line"></span><br><span class="line">name=$(basename $0)</span><br><span class="line">echo</span><br><span class="line">echo The script name is: $name</span><br><span class="line"></span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ bash ~/script_learn/test5.sh</span><br><span class="line"></span><br><span class="line">The script name is: test5.sh</span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ ./test5.sh</span><br><span class="line"></span><br><span class="line">The script name is: test5.sh</span><br></pre></td></tr></table></figure>
<p>下面编写基于脚本名执行不同功能的脚本。挺有意思的，我们看看吧：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx-ubuntu:~/script_learn$ cat test6.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> Testing a Multi-function script</span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">name=$(basename $0)</span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">if [ $name = "addem" ]</span><br><span class="line">then</span><br><span class="line">truetotal=$[ $1 + $2 ]</span><br><span class="line">elif [ $name = "multem" ]</span><br><span class="line">then</span><br><span class="line">truetotal=$[ $1 * $2 ]</span><br><span class="line">fi</span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">echo</span><br><span class="line">echo The calculated value is $total.</span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ cp test6.sh addem</span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ chmod u+x addem</span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ ln -s test6.sh multem</span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ ll *em</span><br><span class="line">-rwxrw-r-- 1 wsx wsx 216 8月  18 16:47 addem*</span><br><span class="line">lrwxrwxrwx 1 wsx wsx   8 8月  18 16:47 multem -&gt; test6.sh*</span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ ./addem 2 5</span><br><span class="line"></span><br><span class="line">The calculated value is 7.</span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ ./multem 2 5</span><br><span class="line"></span><br><span class="line">The calculated value is 10.</span><br></pre></td></tr></table></figure>
<p>这个脚本中创建了两个不同的文件名：一个通过复制创建；另一个通过链接。在两种情况下都会先获得脚本的基本名称，然后根据该值执行相应的功能。</p>
<h3 id="测试参数"><a href="#测试参数" class="headerlink" title="测试参数"></a>测试参数</h3><p>如果在脚本中使用了参数，但是运行脚本时没用参数，这会导致错误发生，需要小心。因此我们在使用参数前应该测试。</p>
<p>好比下面这个例子：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">wsx@wsx-ubuntu:~/script_learn$ cat test7.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> testing parameters before use</span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">if [ -n "$1" ] # -n 选项检查字符是否非空</span><br><span class="line">then</span><br><span class="line">trueecho Hello $1, glad to meet you.</span><br><span class="line">else</span><br><span class="line">trueecho "Sorry, you did not identify yourself."</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ ./test7.sh shixiang</span><br><span class="line">Hello shixiang, glad to meet you.</span><br><span class="line">wsx@wsx-ubuntu:~/script_learn$ ./test7.sh</span><br><span class="line">Sorry, you did not identify yourself.</span><br></pre></td></tr></table></figure>
<h2 id="特殊参数变量"><a href="#特殊参数变量" class="headerlink" title="特殊参数变量"></a>特殊参数变量</h2><p>bash shell中有些特殊变量，它们会记录命令行参数。</p>
<h3 id="参数统计"><a href="#参数统计" class="headerlink" title="参数统计"></a>参数统计</h3><p>bash shell提供了一个特殊的变量<code>$#</code>来统计命令行中输入了多少个参数。我们可以像使用普通变量一样使用它。</p>
<p>现在能在使用参数前测试参数的总数了。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test9.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> Testing parameters</span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">if [ $# -ne 2 ]</span><br><span class="line">then</span><br><span class="line">    echo</span><br><span class="line">    echo Usage: test9.sh a b</span><br><span class="line">    echo</span><br><span class="line">else</span><br><span class="line">    total=$[ $1 + $2 ]</span><br><span class="line">    echo</span><br><span class="line">    echo The total is $total</span><br><span class="line">    echo</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">wangsx@SC-201708020022:~/tmp$ bash test9.sh</span><br><span class="line"></span><br><span class="line">Usage: test9.sh a b</span><br><span class="line"></span><br><span class="line">wangsx@SC-201708020022:~/tmp$ bash test9.sh 10</span><br><span class="line"></span><br><span class="line">Usage: test9.sh a b</span><br><span class="line"></span><br><span class="line">wangsx@SC-201708020022:~/tmp$ bash test9.sh 2 5</span><br><span class="line"></span><br><span class="line">The total is 7</span><br></pre></td></tr></table></figure>
<p>例子中，<code>if-then</code>语句用<code>-ne</code>测试命令行参数数量。如果参数数量不对，会显示一条错误信息告诉脚本的正确用法。</p>
<p>这个变量还提供了一个简便方法来获取命令行最后一个参数，完全不需要知道实际上有多少个参数。不过要实现它需要花些功夫。</p>
<p>也许，我们会有这样的想法，既然<code>$#</code>变量含有参数的总数，那么变量<code>${$#}</code>就代表了最后一个命令行参数变量。试试看呗：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test10.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> testing grabbing last parameter</span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">echo The last parameter was $&#123;$#&#125;</span><br><span class="line"></span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test10.sh 10</span><br><span class="line">The last parameter was 65</span><br></pre></td></tr></table></figure>
<p>我擦，明显不对。这说明<code>${$#}</code>的用法错误。实际上，花括号能不能使用美元符，必须把它换成感叹号。虽然有点奇怪，但的确有用。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test10.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> testing grabbing last parameter</span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">echo The last parameter was $&#123;!#&#125;</span><br><span class="line"></span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test10.sh 10</span><br><span class="line">The last parameter was 10</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test10.sh 1 2 3 4 5</span><br><span class="line">The last parameter was 5</span><br></pre></td></tr></table></figure>
<h3 id="抓取所有的数据"><a href="#抓取所有的数据" class="headerlink" title="抓取所有的数据"></a>抓取所有的数据</h3><p><code>$*</code>与<code>$@</code>变量可以用来轻松访问所有的参数。它们都能够在单个变量中存储所有的命令行参数。</p>
<p><code>$*</code>变量会将命令行提供的所有参数当作一个单词保存。这个单词包含了命令行中出现的每一个参数值。基本上<code>$*</code>变量会将这些参数视为一个整体，而不是多个个体。</p>
<p><code>$@</code>则将所有参数当作同一字符串中的多个独立的单词。这样能够遍历所有的参数值，得到每个参数。这通常交给<code>for</code>命令完成。</p>
<p>弄个例子吧，理解两者的区别。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test11.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> testing $* and <span class="variable">$@</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">echo</span><br><span class="line">echo "Using then \$* method: $*"</span><br><span class="line">echo</span><br><span class="line">echo "Using the \$@ method: $@"</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test11.sh rich barbara katie jessica</span><br><span class="line"></span><br><span class="line">Using then $* method: rich barbara katie jessica</span><br><span class="line"></span><br><span class="line">Using the $@ method: rich barbara katie jessica</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 下面给出两者差异</span></span><br><span class="line">wangsx@SC-201708020022:~/tmp$ cat test12.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line">echo</span><br><span class="line">count=1</span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">for param in "$*"</span><br><span class="line">do</span><br><span class="line">    echo "\$* Parameter #$count = $param"</span><br><span class="line">    count=$[ $count + 1 ]</span><br><span class="line">done</span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">echo</span><br><span class="line">count=1</span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line"></span><br><span class="line">for param in "$@"</span><br><span class="line">do</span><br><span class="line">    echo "\$@ Parameter #$count = $param"</span><br><span class="line">    count=$[ $count + 1 ]</span><br><span class="line">done</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test12.sh rich barbara katie jessica</span><br><span class="line"></span><br><span class="line"><span class="meta">$</span><span class="bash">* Parameter <span class="comment">#1 = rich barbara katie jessica</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">$</span><span class="bash">@ Parameter <span class="comment">#1 = rich</span></span></span><br><span class="line"><span class="meta">$</span><span class="bash">@ Parameter <span class="comment">#2 = barbara</span></span></span><br><span class="line"><span class="meta">$</span><span class="bash">@ Parameter <span class="comment">#3 = katie</span></span></span><br><span class="line"><span class="meta">$</span><span class="bash">@ Parameter <span class="comment">#4 = jessica</span></span></span><br></pre></td></tr></table></figure>
<h2 id="移动变量"><a href="#移动变量" class="headerlink" title="移动变量"></a>移动变量</h2><p>bash shell的<code>shift</code>命令能够用来操作命令行参数。默认情况下它会将每个参数变量向左移动一个位置。So, 变量<code>$3</code>的值会移到<code>$2</code>，<code>$2</code>移到<code>$1</code>，而<code>$1</code>的值会被删除（<code>$0</code>的值，也就是脚本名，是不会变的）。</p>
<p>这是遍历命令行参数的另一个好方法，不需要知道参数的个数，我们只需要操作第一个参数，然后移动参数，继续操作第一个参数。</p>
<p>下面解释它怎么工作的：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test13.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> demonstrating the <span class="built_in">shift</span> <span class="built_in">command</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">echo</span><br><span class="line">count=1</span><br><span class="line">while [ -n "$1" ]</span><br><span class="line">do</span><br><span class="line">    echo "Parameter #$count = $1"</span><br><span class="line">    count=$[ $count + 1 ]</span><br><span class="line">    shift</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test13.sh rich barbara katie jessica</span><br><span class="line"></span><br><span class="line">Parameter #1 = rich</span><br><span class="line">Parameter #2 = barbara</span><br><span class="line">Parameter #3 = katie</span><br><span class="line">Parameter #4 = jessica</span><br></pre></td></tr></table></figure>
<blockquote>
<p>使用shift命令时需要注意，一旦参数被移除，它的值就被丢弃了 ，无法再恢复。</p>
</blockquote>
<p><code>shift n</code>中<code>n</code>可以指定移动多个位置。</p>
<h2 id="处理选项"><a href="#处理选项" class="headerlink" title="处理选项"></a>处理选项</h2><p>熟悉Linux的朋友必然见过不少同时提供参数和选项的bash命令。选项是跟在但破折线后面的单个字母，它能改变命令的行为。下面介绍3中再脚本中处理选项的方法。</p>
<h3 id="查找选项"><a href="#查找选项" class="headerlink" title="查找选项"></a>查找选项</h3><p>只要我们愿意，我们可以像处理命令行参数一样处理命令行选项。</p>
<h4 id="处理简单选项"><a href="#处理简单选项" class="headerlink" title="处理简单选项"></a>处理简单选项</h4><p>前面一节最后我们用<code>shift</code>命令来依次处理脚本携带的命令行参数。我们也可以用同样的方法来处理命令行选项。</p>
<p>在提取每个单独参数时，用<code>case</code>语句来判断某个参数是否为选项。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test15.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> extracting <span class="built_in">command</span> line option as parameter</span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">echo</span><br><span class="line">while [ -n "$1" ]</span><br><span class="line">do</span><br><span class="line">    case "$1" in</span><br><span class="line">        -a) echo "Found the -a option";;</span><br><span class="line">        -b) echo "Found the -b option";;</span><br><span class="line">        -c) echo "Found the -c option";;</span><br><span class="line">        *)  echo "$1 is not an option";;</span><br><span class="line">    esac</span><br><span class="line">    shift</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test15.sh -a -b -c -d</span><br><span class="line"></span><br><span class="line">Found the -a option</span><br><span class="line">Found the -b option</span><br><span class="line">Found the -c option</span><br><span class="line">-d is not an option</span><br></pre></td></tr></table></figure>
<p><code>case</code>语句在命令行参数中找到一个选项，就处理一个选项。如果命令行上还提供了其他参数，你可以在<code>case</code>语句的通用情况处理部分中处理。</p>
<h4 id="分离参数与选项"><a href="#分离参数与选项" class="headerlink" title="分离参数与选项"></a>分离参数与选项</h4><p>shell脚本通常使用选项和参数，Linux中处理这个问题的标准方法是用<strong>特殊字符</strong>来将二者分开，该字符会告诉脚本何时选项结束以及普通参数何时开始。</p>
<p>这个所谓的特殊字符就是<code>--</code>双破折号。</p>
<p>要检查双破折号，在<code>case</code>语句中加一项就行了。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test16.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> extracting options and paramters</span></span><br><span class="line">echo</span><br><span class="line">while [ -n "$1" ]</span><br><span class="line">do</span><br><span class="line">    case "$1" in</span><br><span class="line">        -a) echo "Found the -a option";;</span><br><span class="line">        -b) echo "Found the -b option";;</span><br><span class="line">        -c) echo "Found the -c option";;</span><br><span class="line">        --) shift</span><br><span class="line">            break ;;</span><br><span class="line">        *) echo "$1 is not an option";;</span><br><span class="line">    esac</span><br><span class="line">    shift</span><br><span class="line">done</span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">count=1</span><br><span class="line">for param in $@</span><br><span class="line">do</span><br><span class="line">    echo "Parameter #$count: $param"</span><br><span class="line">    count=$[ $count + 1 ]</span><br><span class="line">done</span><br></pre></td></tr></table></figure>
<p>当遇到双破折号时，先把它移除掉，然后跳出循环，这样shell就会把后面的参数当参数而不是选项处理了。</p>
<p>先用一组普通的选项和参数来运行测试脚本：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ ./test16.sh -c -a -b test1 test2 test3</span><br><span class="line"></span><br><span class="line">Found the -c option</span><br><span class="line">Found the -a option</span><br><span class="line">Found the -b option</span><br><span class="line">test1 is not an option</span><br><span class="line">test2 is not an option</span><br><span class="line">test3 is not an option</span><br></pre></td></tr></table></figure>
<p>现在加上双破折号，进行测试：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ ./test16.sh -c -a -b -- test1 test2 test3</span><br><span class="line"></span><br><span class="line">Found the -c option</span><br><span class="line">Found the -a option</span><br><span class="line">Found the -b option</span><br><span class="line">Parameter #1: test1</span><br><span class="line">Parameter #2: test2</span><br><span class="line">Parameter #3: test3</span><br></pre></td></tr></table></figure>
<p>可以看到，当脚本遇到双破折号时，它会停止处理选项，并将剩下的参数都当做命令处理。</p>
<p>这样如果顺序填写选项和参数的话，显然没什么问题。但是如果乱序写呢？很显然选项和参数对应不起来。如何解决？</p>
<h4 id="处理带值的选项"><a href="#处理带值的选项" class="headerlink" title="处理带值的选项"></a>处理带值的选项</h4><p>有些选项会带上一个额外的参数值，类似下面：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./testing.sh -a test1 -b -c -d test2</span><br></pre></td></tr></table></figure>
<p>下面看看怎么正确处理。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test17.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> extracting <span class="built_in">command</span> line options and values</span></span><br><span class="line">echo</span><br><span class="line">while [ -n "$1" ]</span><br><span class="line">do</span><br><span class="line">    case "$1" in</span><br><span class="line">        -a) echo "Found the -a option";;</span><br><span class="line">        -b) param="$2"</span><br><span class="line">            echo "Found the -b option, with parameter value $param"</span><br><span class="line">            shift;;</span><br><span class="line">        -c) echo "Found the -c option";;</span><br><span class="line">        --) shift</span><br><span class="line">            break;;</span><br><span class="line">        *) echo "$1 is not an option";;</span><br><span class="line">    esac</span><br><span class="line">    shift</span><br><span class="line">done</span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">count=1</span><br><span class="line">for param in "$@"</span><br><span class="line">do</span><br><span class="line">    echo "Paramter #$count: $param"</span><br><span class="line">    count=$[ $count + 1 ]</span><br><span class="line">done</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test17.sh -a -b test1 -d</span><br><span class="line"></span><br><span class="line">Found the -a option</span><br><span class="line">Found the -b option, with parameter value test1</span><br><span class="line">-d is not an option</span><br></pre></td></tr></table></figure>
<p>本例中，定义了3个要处理的选项，<code>-b</code>还带一个额外参数。因为处理的选项是<code>$1</code>，所以额外参数位于<code>$2</code>，另外因为加了额外参数，所以找到后应该用<code>shift</code>把它移除（这个选项占了两个位置，需要多移动一个）。</p>
<p>这样，我们可以根据需求进行类似的设定了。不管什么顺序放置选项都可以正常工作。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ ./test17.sh  -b test1 -a -d</span><br><span class="line"></span><br><span class="line">Found the -b option, with parameter value test1</span><br><span class="line">Found the -a option</span><br><span class="line">-d is not an option</span><br></pre></td></tr></table></figure>
<p>不过，这里还有一些限制。如果我们想把多个选项放在一起，这样就行不通啦~</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ ./test17.sh  -ac</span><br><span class="line"></span><br><span class="line">-ac is not an option</span><br></pre></td></tr></table></figure>
<p>而这种功能是Linux常见的喔。那究竟怎么合并选项呢？幸好还有一种处理方法可以帮忙。</p>
<h3 id="使用getopt命令"><a href="#使用getopt命令" class="headerlink" title="使用getopt命令"></a>使用getopt命令</h3><p><code>getopt</code>命令是一个处理命令行选项和参数时非常方便的工具。它能够识别命令行参数，从而更方便地进行解析。</p>
<p><strong>命令的格式</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">getopt optstring parameters</span><br></pre></td></tr></table></figure>
<p><code>optstring</code>是这个过程的关键所在。它定义了命令行有效的选项字母，还定义了哪些字母需要带参数。</p>
<p><strong>首先，在<code>optstring</code>中列出你要在脚本中用到的每个命令行选项字母。然后，在每个需要参数值的选项字母后加一个冒号。</strong></p>
<blockquote>
<p><code>getopt</code>的高级版本叫<code>getoptions</code>。需要注意区分</p>
</blockquote>
<p>下面看看<code>getopt</code>如何工作的：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ getopt ab:cd -a -b test1 -cd test2 test3</span><br><span class="line"> -a -b test1 -c -d -- test2 test3</span><br></pre></td></tr></table></figure>
<p>运行完后看到结果感觉自己晕乎乎的，让我们一起来看看解释：</p>
<p><code>optstring</code>定义了四个有效选项字母：a,b,c,d。（嗯，对的，这个没问题）。冒号被放在字母b后面，说明b选项需要一个参数。（这样啊）。当<code>getopt</code>命令运行时，它会检查参数列表（就时getopt命令后面跟的），并基于提供的<code>optstring</code>进行解析。值得注意，它会自动把<code>-cd</code>选项分成两个独立的选项，并插入双破折号来分隔行中的额外参数。</p>
<p>如果指定的选项不在<code>optstring</code>中，会报错。<code>-q</code>选项可以忽略掉它（注意放在<code>optstring</code>之前，因为是命令本身的选项嘛）。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ getopt ab:cd -a -b test1 -cde test2 test3</span><br><span class="line">getopt：无效选项 -- e</span><br><span class="line"> -a -b test1 -c -d -- test2 test3</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ getopt -q ab:cd -a -b test1 -cde test2 test3</span><br><span class="line"> -a -b 'test1' -c -d -- 'test2' 'test3'</span><br></pre></td></tr></table></figure>
<p><strong>在脚本中用getopt</strong></p>
<p>用法稍微有点复杂，方法是用<code>getopt</code>命令生成的格式化后的版本替换已有的命令行选项和参数。用<code>set</code>可以做到。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">set -- $(getopt -q ab:cd "$@)</span><br></pre></td></tr></table></figure>
<p><code>set</code>命令的选项之一是<code>--</code>，它会将命令行参数替换成<code>set</code>命令的命令行值。</p>
<p>该方法会将原始脚本的命令行参数传给<code>getopt</code>命令，之后将<code>getopt</code>命令的输出传给<code>set</code>命令，用<code>getopt</code>格式化的命令行参数来替换原始的命令行参数。</p>
<p>现在写下处理命令行参数的脚本吧：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test18.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> Extract <span class="built_in">command</span> line options &amp; values with getopt</span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">set -- $(getopt -q ab:cd "$@")</span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">echo</span><br><span class="line">while [ -n "$1" ]</span><br><span class="line">do</span><br><span class="line">    case "$1" in</span><br><span class="line">        -a) echo "Found the -a option";;</span><br><span class="line">        -b) param="$2"</span><br><span class="line">            echo "Found the -b option, with parameter value $param"</span><br><span class="line">            shift ;;</span><br><span class="line">        -c) echo "Found the -c option";;</span><br><span class="line">        --) shift</span><br><span class="line">            break ;;</span><br><span class="line">        *) echo "$1 is not an option"</span><br><span class="line">    esac</span><br><span class="line">    shift</span><br><span class="line">done</span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">count=1</span><br><span class="line">for param in "$@"</span><br><span class="line">do</span><br><span class="line">    echo "Parameter #$count: $param"</span><br><span class="line">    count=$[ $count + 1 ]</span><br><span class="line">done</span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br></pre></td></tr></table></figure>
<p>可以看到它跟<code>test17.sh</code>不同的地方是加入了<code>getopt</code>命令来帮助格式化命令行参数。</p>
<p>下面测试发现新加的功能实现了，之前的也没问题。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ ./test18.sh -ac</span><br><span class="line"></span><br><span class="line">Found the -a option</span><br><span class="line">Found the -c option</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test18.sh -a -b test1 -cd  test2 test3 test4</span><br><span class="line"></span><br><span class="line">Found the -a option</span><br><span class="line">Found the -b option, with parameter value 'test1'</span><br><span class="line">Found the -c option</span><br><span class="line">-d is not an option</span><br><span class="line">Parameter #1: 'test2'</span><br><span class="line">Parameter #2: 'test3'</span><br><span class="line">Parameter #3: 'test4'</span><br></pre></td></tr></table></figure>
<p>相当不错啦。不过<code>getopt</code>命令隐藏一个问题。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ ./test18.sh -a -b test1 -cd  "test2 test3" test4</span><br><span class="line"></span><br><span class="line">Found the -a option</span><br><span class="line">Found the -b option, with parameter value 'test1'</span><br><span class="line">Found the -c option</span><br><span class="line">-d is not an option</span><br><span class="line">Parameter #1: 'test2</span><br><span class="line">Parameter #2: test3'</span><br><span class="line">Parameter #3: 'test4'</span><br></pre></td></tr></table></figure>
<p><code>getopt</code>命令并不擅长处理带空格和引号的参数值。它会将空格当作参数分隔符，而不是根据双引号将两者当作一个参数。</p>
<p>幸而还有办法能够解决这个问题。</p>
<h3 id="使用更高级的getopts"><a href="#使用更高级的getopts" class="headerlink" title="使用更高级的getopts"></a>使用更高级的getopts</h3><p><code>getopts</code>命令内建于bash shell。它比<code>getopt</code>多一些扩展功能。</p>
<p><code>getopts</code>命令格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">getopts optstring variable</span><br></pre></td></tr></table></figure>
<p><code>optstring</code>值类似于<code>getopt</code>命令中的那个。要去掉错误信息的话，可以在<code>optstring</code>之前加一个冒号。<code>getopts</code>命令将当前参数保存在命令行中定义的<code>variable</code>中。</p>
<p><strong>该命令会用到两个环境变量。如果选项需要跟一个参数值，<code>OPTARG</code>环境变量就会保存这个值。<code>OPTIND</code>环境变量保存了参数列表中<code>getopts</code>正在处理的参数位置。这样你就能在处理选项之后继续处理其他命令行参数了。</strong></p>
<p>空说无益，还是来练练。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test19.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> Simple demonstration of the <span class="built_in">getopts</span> <span class="built_in">command</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">echo</span><br><span class="line">while getopts :ab:c opt</span><br><span class="line">do</span><br><span class="line">    case "$opt" in</span><br><span class="line">       a) echo "Found the -a option" ;;</span><br><span class="line">       b) echo "Found the -b option, with value $OPTARG";;</span><br><span class="line">       c) echo "Found the -c option";;</span><br><span class="line">       *) echo "Unknown option: $opt";;</span><br><span class="line">   esac</span><br><span class="line">done</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test19.sh -ab test1 -c</span><br><span class="line"></span><br><span class="line">Found the -a option</span><br><span class="line">Found the -b option, with value test1</span><br><span class="line">Found the -c option</span><br></pre></td></tr></table></figure>
<p><code>while</code>语句定义了<code>getopts</code>命令，指明了要查找哪些命令行选项，以及每次迭代中存储它们的变量名（<code>opt</code>）。</p>
<p><code>getopts</code>运行时，它一次只处理命令行上检测到的一个参数。处理完所有参数后，它会退出并返回一个大于0的退出状态码。这让它非常适合用于解析命令行所有的参数的循环中。</p>
<p>这里我们可以已经注意到了例子中的<code>case</code>用法和之前不同。<code>getopts</code>命令解析命令行选项时会移除开头的单破折号，所以在<code>case</code>定义中不用单破折号了。</p>
<p>上一小节末尾我们遇到的问题可以很好的解决了：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ ./test19.sh -ab "test1 test2" -c</span><br><span class="line"></span><br><span class="line">Found the -a option</span><br><span class="line">Found the -b option, with value test1 test2</span><br><span class="line">Found the -c option</span><br></pre></td></tr></table></figure>
<p>另一个好用的功能是能够将选项字母和参数值放在一起，而且不用加空格。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ ./test19.sh -abtest1</span><br><span class="line"></span><br><span class="line">Found the -a option</span><br><span class="line">Found the -b option, with value test1</span><br></pre></td></tr></table></figure>
<p>除此之外，<code>getopts</code>还能够将命令行上找到的所有未定义的选项统一输出成问号。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ ./test19.sh -d</span><br><span class="line"></span><br><span class="line">Unknown option: ?</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test19.sh -acde</span><br><span class="line"></span><br><span class="line">Found the -a option</span><br><span class="line">Found the -c option</span><br><span class="line">Unknown option: ?</span><br><span class="line">Unknown option: ?</span><br></pre></td></tr></table></figure>
<p><code>getopts</code>命令知道何时停止处理选项，并将参数留给你处理。在<code>getopts</code>处理每一个选项时，它会将<code>OPTIND</code>环境变量值增一。在<code>getopts</code>完成处理后，你可以使用<code>shift</code>命令和<code>OPTIND</code>值来移动参数。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test20.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> Processing options &amp; parameters with <span class="built_in">getopts</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">echo</span><br><span class="line">while getopts :ab:cd opt</span><br><span class="line">do</span><br><span class="line">    case "$opt" in</span><br><span class="line">        a) echo "Found the -a option" ;;</span><br><span class="line">        b) echo "Found the -a option, with value $OPTARG" ;;</span><br><span class="line">        c) echo "Found the -c option" ;;</span><br><span class="line">        d) echo "Found the -d option" ;;</span><br><span class="line">        *) echo "Unknown option: $opt" ;;</span><br><span class="line">    esac</span><br><span class="line">done</span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">shift $[ $OPTIND -1 ]</span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">echo</span><br><span class="line">count=1</span><br><span class="line">for param in  "$@"</span><br><span class="line">do</span><br><span class="line">    echo "Parameter $count: $param"</span><br><span class="line">    count=$[ $count + 1 ]</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test20.sh -a -b test1 -d test2 test3 test4</span><br><span class="line"></span><br><span class="line">Found the -a option</span><br><span class="line">Found the -a option, with value test1</span><br><span class="line">Found the -d option</span><br><span class="line"></span><br><span class="line">Parameter 1: test2</span><br><span class="line">Parameter 2: test3</span><br><span class="line">Parameter 3: test4</span><br></pre></td></tr></table></figure>
<p>这里<code>shift $[ $OPTIND -1 ]</code>需要解释以下：前面提到<code>OPTIND</code>在<code>getopts</code>每次处理掉一个参数后会加1。比如<code>./test20.sh -a -b test1 -d test2 test3 test4</code>前面键入了4个参数，选项处理完成后<code>OPTIND</code>的值为5。它会指向<code>$5</code>，即第5个参数，后面为了值剩下命令行参数，所以去掉所有的选项（及带的参数），所以用<code>shift $[ $OPTIND - 1]</code>命令。</p>
<h2 id="将选项标准化"><a href="#将选项标准化" class="headerlink" title="将选项标准化"></a>将选项标准化</h2><p>有些字母在Linux世界里已经拥有了某种程度的标准含义。如果我们能在shellji奥本中支持这些选项，脚本看起来会更加友好。</p>
<p>下面表格列出一些命令行选项的常用含义</p>
<table>
<thead>
<tr>
<th style="text-align:center">选项</th>
<th style="text-align:center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">-a</td>
<td style="text-align:center">显示所有对象</td>
</tr>
<tr>
<td style="text-align:center">-c</td>
<td style="text-align:center">生成一个计数</td>
</tr>
<tr>
<td style="text-align:center">-d</td>
<td style="text-align:center">指定一个目录</td>
</tr>
<tr>
<td style="text-align:center">-e</td>
<td style="text-align:center">扩展一个对象</td>
</tr>
<tr>
<td style="text-align:center">-f</td>
<td style="text-align:center">指定读入数据的文件</td>
</tr>
<tr>
<td style="text-align:center">-h</td>
<td style="text-align:center">显示命令的帮助信息</td>
</tr>
<tr>
<td style="text-align:center">-i</td>
<td style="text-align:center">忽略文本大小写</td>
</tr>
<tr>
<td style="text-align:center">-l</td>
<td style="text-align:center">产生输出的长格式版本</td>
</tr>
<tr>
<td style="text-align:center">-n</td>
<td style="text-align:center">使用非交互模式（批处理）</td>
</tr>
<tr>
<td style="text-align:center">-o</td>
<td style="text-align:center">将所有输出重定向到指定的输出文件</td>
</tr>
<tr>
<td style="text-align:center">-q</td>
<td style="text-align:center">以安静模式运行</td>
</tr>
<tr>
<td style="text-align:center">-r</td>
<td style="text-align:center">递归地处理目录和文件</td>
</tr>
<tr>
<td style="text-align:center">-s</td>
<td style="text-align:center">以安静模式运行</td>
</tr>
<tr>
<td style="text-align:center">-v</td>
<td style="text-align:center">生成详细输出</td>
</tr>
<tr>
<td style="text-align:center">-x</td>
<td style="text-align:center">排除某个对象</td>
</tr>
<tr>
<td style="text-align:center">-y</td>
<td style="text-align:center">对所有问题答yes</td>
</tr>
</tbody>
</table>
<h2 id="获得用户输入"><a href="#获得用户输入" class="headerlink" title="获得用户输入"></a>获得用户输入</h2><p>有时脚本地交互性还需要更强一些。比如你想要在脚本运行时问个问题，并等待运行脚本地人来回答。bash shell为此提供了read命令。</p>
<h3 id="基本的读入"><a href="#基本的读入" class="headerlink" title="基本的读入"></a>基本的读入</h3><p><code>read</code>命令从标准输入（键盘）或另一个文件描述符中接受输入。在收到输入后，<code>read</code>命令会将数据放进一个变量。</p>
<p>简单用法如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test21.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> testing the <span class="built_in">read</span> -p option</span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">read -p "Please enter your age: " age</span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">days=$[ $age * 365 ]</span><br><span class="line">echo "That makes you over $days days old!"</span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test21.sh</span><br><span class="line">Please enter your age: 23</span><br><span class="line">That makes you over 8395 days old!</span><br></pre></td></tr></table></figure>
<p><code>read</code>命令会将提示符后输入的所有数据分配给单个变量，要么我们需要指定多个变量。当变量数量不够时，剩下的数据就全部分配给最后一个变量。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test22.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> entering multiple variable</span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">read -p "Enter your name: " first last</span><br><span class="line">echo "Checking data for $last, $first..."</span><br><span class="line"></span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test22.sh</span><br><span class="line">Enter your name: shixiang wang</span><br><span class="line">Checking data for wang, shixiang...</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test22.sh</span><br><span class="line">Enter your name: shixiang wang hhhhh</span><br><span class="line">Checking data for wang hhhhh, shixiang...</span><br></pre></td></tr></table></figure>
<p>也可以在<code>read</code>命令行中不指定变量。如果这样的话，<code>read</code>命令会将它收到的任何数据都放进特殊环境变量<code>REPLY</code>中。我们直接可以使用它。</p>
<h3 id="超时"><a href="#超时" class="headerlink" title="超时"></a>超时</h3><p>脚本很可能一直苦苦等待脚本用户的输入。如果不管数据是否输入，脚本都执行的话，我们可以用<code>-t</code>选项设定一个计时器。<code>-t</code>指定<code>read</code>命令等待的秒数，计数完成后，<code>read</code>命令会返回一个非零退出状态码。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test23.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> timing the data entry</span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">if read -t 5 -p "Please enter your name: " name</span><br><span class="line">then</span><br><span class="line">    echo "Hello $name, welcome to my script"</span><br><span class="line">else</span><br><span class="line">    echo</span><br><span class="line">    echo "Sorry, too slow!"</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test23.sh</span><br><span class="line">Please enter your name: shixiang wang</span><br><span class="line">Hello shixiang wang, welcome to my script</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test23.sh # 这里输入后等以下 不要输入</span><br><span class="line">Please enter your name:</span><br><span class="line">Sorry, too slow!</span><br></pre></td></tr></table></figure>
<p>也可以不对输入过程计时，而时让<code>read</code>命令来统计输入的字符数。当输入的字符数达到预设的字符数时，就自动退出，将输入的数据赋值给变量。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test24.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> getting just one character of input</span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">read -n1 -p "Do you want to continue [Y/N]? " answer</span><br><span class="line">case $answer in</span><br><span class="line">    Y | y ) echo</span><br><span class="line">            echo "fine, continue on...";;</span><br><span class="line">    N | n ) echo</span><br><span class="line">            echo OK, goodbye</span><br><span class="line">            exit;;</span><br><span class="line">esac</span><br><span class="line">echo "This is the end of the script."</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test24.sh</span><br><span class="line">Do you want to continue [Y/N]? Y</span><br><span class="line">fine, continue on...</span><br><span class="line">This is the end of the script.</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test24.sh</span><br><span class="line">Do you want to continue [Y/N]? n</span><br><span class="line">OK, goodbye</span><br></pre></td></tr></table></figure>
<h3 id="隐藏方式读取"><a href="#隐藏方式读取" class="headerlink" title="隐藏方式读取"></a>隐藏方式读取</h3><p>这种方式输入密码的时候有用。</p>
<p><code>-s</code>选项可以避免在<code>read</code>命令输入的数据出现在显示器上（实际上，数据会被显示，只是<code>read</code>命令会将文本颜色设成跟背景色一样）。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test25.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> hiding input data from the monitor</span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">read -s -p "Enter your password: " pass</span><br><span class="line">echo</span><br><span class="line">echo "Is your password really $pass?"</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test25.sh</span><br><span class="line">Enter your password:</span><br><span class="line">Is your password really what?</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test25.sh</span><br><span class="line">Enter your password:</span><br><span class="line">Is your password really shixiang?</span><br></pre></td></tr></table></figure>
<h3 id="从文件中读取"><a href="#从文件中读取" class="headerlink" title="从文件中读取"></a>从文件中读取</h3><p>当然，<code>read</code>也可以从系统文件中读取数据。每次调用<code>read</code>命令，它都会读取一行文本。当读完后，<code>read</code>命令会退出并返回非零退出状态码。</p>
<p>最常见的方法时对文本使用<code>cat</code>命令，将结果通过管道直接传给含<code>read</code>命令的<code>while</code>命令。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">wangsx@SC-201708020022:~/tmp$ cat test26.sh</span><br><span class="line"><span class="meta">#</span><span class="bash">!/bin/bash</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> reading data from a file</span></span><br><span class="line"><span class="meta">#</span><span class="bash"></span></span><br><span class="line">count=1</span><br><span class="line">cat test | while read line</span><br><span class="line">do</span><br><span class="line">    echo "Line $count: $line"</span><br><span class="line">    count=$[ $count + 1 ]</span><br><span class="line">done</span><br><span class="line">echo "Finished processing the file"</span><br><span class="line"></span><br><span class="line">wangsx@SC-201708020022:~/tmp$ cat test</span><br><span class="line">The quick brown dog jumps over the lazy fox.</span><br><span class="line">This is a test, this is only a test.</span><br><span class="line">O Romeo, Romeo! Wherefore art thou Romeo?</span><br><span class="line">wangsx@SC-201708020022:~/tmp$ ./test26.sh</span><br><span class="line">Line 1: The quick brown dog jumps over the lazy fox.</span><br><span class="line">Line 2: This is a test, this is only a test.</span><br><span class="line">Line 3: O Romeo, Romeo! Wherefore art thou Romeo?</span><br><span class="line">Finished processing the file</span><br></pre></td></tr></table></figure>
<hr>
<p>写shell脚本的基本内容大体已经整完了，我自己也是边看边想边码过来的。shell博大精深，更多高级内容有待继续学习整理。码字实属不易，觉得内容还行的点赞支持下吧~</p>

      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="">Shell脚本之处理用户输入</a></p>
        <!-- modify author information
        <p><span>文章作者:</span><a href="/" title="回到主页">王诗翔</a></p>
        -->
        <p><span>文章作者:</span>王诗翔</p>
        <p><span>发布时间:</span>2017-08-19, 00:00:00</p>
        <p><span>最后更新:</span>2018-01-27, 12:08:11</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="" title="Shell脚本之处理用户输入">https://shixiangwang.github.io/2017/08/19/working-with-user-input/</a>
            <span class="copy-path" data-clipboard-text="原文: https://shixiangwang.github.io/2017/08/19/working-with-user-input/　　作者: 王诗翔" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="../../21/shell-show-data/">
                    Linux脚本编程——呈现数据
                </a>
            </div>
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="../../11/structural-command-of-shell/">
                    Linux结构化命令
                </a>
            </div>
        
    </nav>


  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">文章目录</strong>
        
            <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#命令行参数"><span class="toc-number">1.</span> <span class="toc-text">命令行参数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#读入参数"><span class="toc-number">1.1.</span> <span class="toc-text">读入参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#测试参数"><span class="toc-number">1.2.</span> <span class="toc-text">测试参数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#特殊参数变量"><span class="toc-number">2.</span> <span class="toc-text">特殊参数变量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#参数统计"><span class="toc-number">2.1.</span> <span class="toc-text">参数统计</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#抓取所有的数据"><span class="toc-number">2.2.</span> <span class="toc-text">抓取所有的数据</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#移动变量"><span class="toc-number">3.</span> <span class="toc-text">移动变量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#处理选项"><span class="toc-number">4.</span> <span class="toc-text">处理选项</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#查找选项"><span class="toc-number">4.1.</span> <span class="toc-text">查找选项</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#处理简单选项"><span class="toc-number">4.1.1.</span> <span class="toc-text">处理简单选项</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#分离参数与选项"><span class="toc-number">4.1.2.</span> <span class="toc-text">分离参数与选项</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#处理带值的选项"><span class="toc-number">4.1.3.</span> <span class="toc-text">处理带值的选项</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用getopt命令"><span class="toc-number">4.2.</span> <span class="toc-text">使用getopt命令</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用更高级的getopts"><span class="toc-number">4.3.</span> <span class="toc-text">使用更高级的getopts</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#将选项标准化"><span class="toc-number">5.</span> <span class="toc-text">将选项标准化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#获得用户输入"><span class="toc-number">6.</span> <span class="toc-text">获得用户输入</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#基本的读入"><span class="toc-number">6.1.</span> <span class="toc-text">基本的读入</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#超时"><span class="toc-number">6.2.</span> <span class="toc-text">超时</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#隐藏方式读取"><span class="toc-number">6.3.</span> <span class="toc-text">隐藏方式读取</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#从文件中读取"><span class="toc-number">6.4.</span> <span class="toc-text">从文件中读取</span></a></li></ol></li></ol>
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

    <script>
        yiliaConfig.toc = ["隐藏目录", "显示目录", !!"false"];
    </script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"Shell脚本之处理用户输入　| Cloudberry　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    
        <section id="comments">
    <style> aside.comment-bar { margin: auto 30px; }</style>
    <div id="disqus_thread"></div>
    <script>
        var disqus_config = function(){
            this.page.url = 'https://shixiangwang.github.io/2017/08/19/working-with-user-input/';
            this.page.identifier = '2017/08/19/working-with-user-input/';
        };
        var loadComment = function(){
            var d = document, s = d.createElement('script');
            s.src = '//Cloudberry.disqus.com/embed.js';
            s.setAttribute('data-timestamp', +new Date());
            (d.head || d.body).appendChild(s);
        }
    </script>
    
    <script> loadComment(); </script>

</section>


    




    <div class="scroll" id="post-nav-button">
        
            <a href="../../21/shell-show-data/" title="上一篇: Linux脚本编程——呈现数据">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="../../11/structural-command-of-shell/" title="下一篇: Linux结构化命令">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="../../../../2018/02/26/python-basics/">两天研习Python基础</a></li><li class="post-list-item"><a class="post-list-link" href="../../../../2018/02/12/how-to-create-an-independent-figpath/">为markdown博文创建独立图片路径</a></li><li class="post-list-item"><a class="post-list-link" href="../../../../2018/02/10/easy-sapply-apply-vapply/">简单理解lapply,sapply,vapply</a></li><li class="post-list-item"><a class="post-list-link" href="../../../../2018/02/07/how-to-do-group-survival-analysis/">怎么对连续变量分组并进行生存分析</a></li><li class="post-list-item"><a class="post-list-link" href="../../../../2018/02/06/how-to-write-rmd-documents-in-hexo-system/">怎么在hexo博客系统中用Rmarkdown写文章</a></li><li class="post-list-item"><a class="post-list-link" href="../../../../2018/01/31/sed-how-to-filter-rows-before-using-command/">sed如何在执行命令前过滤特定文本行</a></li><li class="post-list-item"><a class="post-list-link" href="../../../../2018/01/31/sync-deploy-tools/">Sync deploy 命令工具</a></li><li class="post-list-item"><a class="post-list-link" href="../../../12/25/sed-and-gawk/">初识sed与awk</a></li><li class="post-list-item"><a class="post-list-link" href="../../../12/08/Git-basic-operation/">学习git</a></li><li class="post-list-item"><a class="post-list-link" href="../../../11/29/shell-create-text-menu-and-window/">使用shell创建文本菜单和窗口部件</a></li><li class="post-list-item"><a class="post-list-link" href="../../../11/26/shell-create-function/">创建和使用shell函数</a></li><li class="post-list-item"><a class="post-list-link" href="../../../09/20/OOPinR/">R-面向对象编程</a></li><li class="post-list-item"><a class="post-list-link" href="../../../09/15/Class-in-R/">R中的属性与类</a></li><li class="post-list-item"><a class="post-list-link" href="../../../09/04/control_shell/">控制脚本</a></li><li class="post-list-item"><a class="post-list-link" href="../../../09/03/Linux-data-analysis-tools/">Linux数据处理命令工具</a></li><li class="post-list-item"><a class="post-list-link" href="../../21/shell-show-data/">Linux脚本编程——呈现数据</a></li><li class="post-list-item"><a class="post-list-link" href="">Shell脚本之处理用户输入</a></li><li class="post-list-item"><a class="post-list-link" href="../../11/structural-command-of-shell/">Linux结构化命令</a></li><li class="post-list-item"><a class="post-list-link" href="../../11/basic-shell/">构建基本shell脚本</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2017-2018 王诗翔
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的博客框架">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减 Hexo 双栏博客主题  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="本站到访数"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="本页阅读量"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
    <script src="../../../../js/GithubRepoWidget.js"></script>

<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>

    <script>
        $(document).ready(function() {
            var iPad = window.navigator.userAgent.indexOf('iPad');
            if (iPad > -1 || $(".left-col").css("display") === "none") {
                var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
                var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
                $("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
            }
            else {
                var backgroundnum = 2;
                var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
                $("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
            }
        })
    </script>





    <script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.6.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


<div class="scroll" id="scroll">
    <a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="查看评论"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        var oOpenInNew = {
             github: ".github-widget a", 
            
             post: ".article-entry a[href], .copyright a[href]", 
            
            
            
             archives: ".archive-article-title", 
             miniArchives: "a.post-list-link", 
            
             friends: "#js-friends a", 
             socail: ".social a" 
        }
        for (var x in oOpenInNew) {
            $(oOpenInNew[x]).attr("target", "_blank");
        }
    
</script>

    <script>
        var originTitle = document.title;
        var titleTime;
        document.addEventListener("visibilitychange", function() {
            if (document.hidden) {
                document.title = "(つェ⊂) 我藏好了哦~ " + originTitle;
                clearTimeout(titleTime);
            }
            else {
                document.title = "(*´∇｀*) 被你发现啦~ " + originTitle;
                titleTime = setTimeout(function() {
                    document.title = originTitle;
                }, 2000);
            }
        })
    </script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>